package doubleCS.client;

import doubleCS.dataService.ConnectMySql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author fengke
 */
public class ClientGui {
    public static void main(String[] args) throws SQLException {
        System.out.println("--------个人通讯录简易版--------");
        Scanner input = new Scanner(System.in);
        System.out.println("1.查看联系人信息");
        System.out.println("2.添加新联系人");
        System.out.println("3.修改联系人信息");
        System.out.println("4.删除联系人");
        System.out.println("--------请输入--------");
        while (true) {
            int choose = input.nextInt();
            judgeChose(choose);
        }
    }

    public static void judgeChose(int choose) throws SQLException {
        if (choose == 1) {
            select("select * from test.PersonalAddressBook;");
            System.out.println("--------请输入--------");
        } else if (choose == 2) {
            Scanner input = new Scanner(System.in);
            String phoneNumber, address, name;
            System.out.println("--------请输入电话号码--------");
            phoneNumber = input.next();
            System.out.println("--------请输入地址--------");
            address = input.next();
            System.out.println("--------请输入姓名--------");
            name = input.next();
            String sql = "insert into test.PersonalAddressBook values('"+phoneNumber+"', '"+ address +"', '"+ name +"');";
            update(sql,"添加成功！");
        } else if (choose == 3) {
            Scanner input = new Scanner(System.in);
            System.out.println("--------请输入要修改的联系人姓名--------");
            String name = input.next();
            String phoneNumber, address;
            System.out.println("--------请输入新电话号码--------");
            phoneNumber = input.next();
            System.out.println("--------请输入新地址--------");
            address = input.next();
            String sql = "update test.PersonalAddressBook set phoneNumber='"+phoneNumber+"',address='"+address+"' where name='"+name+"';";
            update(sql,"更新成功！");
        } else if (choose == 4) {
            Scanner input = new Scanner(System.in);
            System.out.println("--------请输入要删除的联系人姓名--------");
            String name = input.next();
            String sql = "delete from test.PersonalAddressBook where name='"+name+"';";
            update(sql,"删除成功！");
        }
    }

    public static void select(String sql) throws SQLException {
        ConnectMySql con = new ConnectMySql();
        con.openMySql();
        ResultSet resultSet = con.select(sql);
        print(resultSet);
        resultSet.close();
        con.closeMySql();
    }

    public static void update(String sql, String message) throws SQLException {
        ConnectMySql con = new ConnectMySql();
        con.openMySql();
        con.update(sql);
        System.out.println(message);
        ResultSet resultSet = con.select("select * from test.PersonalAddressBook;");
        print(resultSet);
        resultSet.close();
        con.closeMySql();
    }

    public static void print(ResultSet resultSet) throws SQLException {
        while (resultSet.next()) {
            String phoneNumber = resultSet.getString("phoneNumber");
            String address = resultSet.getString("address");
            String name = resultSet.getString("name");

            System.out.println("电话号码:" + phoneNumber + " -- 住址:" + address + " -- 姓名：" + name);
        }
    }
}
